﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using AzureGames.Data;
using AzureGames.Domain.Contracts;
using AzureGames.Domain.Models;

namespace AzureGames.Domain.Handlers
{
    public class GetBoard
    {
        private readonly IBoardRepository _repository;        

        public GetBoard(IBoardRepository gameBoardRepository)
        {
            _repository = gameBoardRepository;            
        }

        public virtual Model.Board Execute(Guid gameId)
        {
            try
            {
                // TODO: a more clever way needs to be done to update the gameboard... 
                //       maybe parse part of the json                  



                // TODO: some form of validation should be performed as it would be very easy at the moment
                //       for someone to cheat by modifying the gameboard JSON
                //       maybe we will record a history of JSON or call out to a class to validate the move
                //       or use node.js to receive and validate and then to call this method if validation 
                //       was successful
                return _repository.GetGameBoard(gameId);
            }
            catch (InvalidOperationException e)
            {
                throw new BusinessServicesException("Unable to retrieve game board from database.", e);
            }
        }
    }
}
